<?php

$cls_name = 'AjaxMode'.ucfirst($_REQUEST['model']);
$act_name = $_REQUEST['act'];
$ajaxModMode = new $cls_name();
$result_data = $ajaxModMode->$act_name();

$json_response =  array('error_message'=>'');
$json_response['data'] = $result_data;

echo $GLOBALS['APP_INSTANCE']->json_encode($json_response);
exit();


class AjaxModeBase
{
	public function __construct()
	{
		
	}
	function doAction($action_name)
	{
		return $this->$action_name();
	}
}
class AjaxmodeArticle extends AjaxmodeBase
{
	public function __construct()
	{
		parent::__construct();
	}
	function get_list_data()
	{
		$dt = new DataTable(constant('DB_TABLE_ARTICLE'));
		$condition = "1=1";
		if ( isset($_REQUEST['is_delete']) ) {
			$condition .= " AND `is_delete`='".Util::sql_safe($_REQUEST['is_delete'])."'";
		}else{
			$condition .= " AND `is_delete`='N'";
		}
		
		if ( isset($_REQUEST['cat_id']) ) {
			$condition .= sprintf(" AND `cat_id`=%d", $_REQUEST['cat_id']);
		}
		if ( isset($_REQUEST['sub_cat_id']) ) {
			$condition .= sprintf(" AND `sub_cat_id`=%d", $_REQUEST['sub_cat_id']);
		}
		if (  isset($_REQUEST['key_word']) && strlen($_REQUEST['key_word']) > 0 ) {
			$condition .= sprintf(" AND `title` like '%%%s%%'", Util::sql_safe($_REQUEST['key_word']));
		}
		$page = isset($_REQUEST['page'])?$_REQUEST['page']:1;
		$page_size = isset($_REQUEST['page_size'])?$_REQUEST['page_size']:10;
		$offset = $page_size * ( $page - 1);
		$dab_list_data = $dt->get_list($condition, array('-date','-id'), $page_size, $offset);
		$list_data = array();
		foreach($dab_list_data as $row)
		{
			$new_row = array();
			$new_row['id'] = $row['id'];
			$new_row['date'] = $GLOBALS['APP_INSTANCE']->formatDate($row['date']);
			if ( $row['cat_id'] == 1 || $row['cat_id'] == 2){
			$new_row['sub_cat'] = $GLOBALS['GV_ARTICLE_SUB_CAT'][$row['cat_id']][$row['sub_cat_id']];
			}else{
				$new_row['sub_cat'] = '下載專區';
			}
			$new_row['title'] = $row['title'];
			$new_row['times'] = $row['visit_times'];
			$new_row['is_delete'] = $row['is_delete'];
			
			$list_data[] = $new_row;
		}
		$total_row = $dt->get_count($condition);
		$total_row = floatval($total_row);
		$total_page = ceil($total_row/$page_size);
		return array('total_page'=>$total_page,'list_data'=>$list_data);
	}
	
	function set_delete()
	{
		$update_data = array('id'=>intval($_REQUEST['id']), 'is_delete'=>'Y');
		$dt = new DataTable(constant('DB_TABLE_ARTICLE'));
		$dt->save($update_data);
		return TRUE;
	}
	function revokedelete()
	{
		$update_data = array('id'=>intval($_REQUEST['id']), 'is_delete'=>'N');
		$dt = new DataTable(constant('DB_TABLE_ARTICLE'));
		$dt->save($update_data);
		return TRUE;
	}
	function delete()
	{
		$dt_image =  new DataTable(constant('DB_TABLE_ARTICLE_IMAGE'));
		$list_data = $dt_image->get_list(sprintf("`article_id`=%d",$_REQUEST['id']));
		foreach($list_data as $row)
		{
			$GLOBALS['APP_INSTANCE']->removeFile($row['path'],'article/');
			$dt_image->remove($row['id']);
		}
		
		$dt = new DataTable(constant('DB_TABLE_ARTICLE'));
		$row = $dt->get($_REQUEST['id']);
		if ( $row ) {
			$GLOBALS['APP_INSTANCE']->removeFile($row['img_path'],'article/');
			$dt->remove($row['id']);
		}
	}
}
?>